%INIT part
clear
[pathstr, name, ext] = fileparts(mfilename('fullpath')); 
cd(pathstr);
mkdir('tmp')
delete('tmp/*');
%Ok, now we are near inside rc directory wich contains all stuff :)
%Clearing plots from previous run
localmode = false;
if matlabpool('size')==0 && localmode==false
    matlabpool('open','AttachedFiles',{'./'})
end
addpath(genpath('./'));
config_path = 'config.xml';
config = xml_load(config_path);

%This is number of tests. Tests are executed sequentionaly and are
%independent
tic;
test_count = length(config.tests);

%t is a counter of atomic input data for regression
%in this loop we generate cell array of struct to compute with.
t = 1;
for tst=1:test_count
    tstconf = config.tests(tst).test;
    disp(['Test name is ',tstconf.name]);
    for fi=1:length(tstconf.functions)
        func = tstconf.functions(fi).function;
        for ri = 1:length(tstconf.randomizers)
            rnd = tstconf.randomizers(ri).randomizer;
            for rni=1:length(tstconf.ranges)
                range = tstconf.ranges(rni).range;
                for ti=1:length(tstconf.testcounts)
                    tstcount = str2double(tstconf.testcounts(ti).n);
                    %writing current input data
                    input{t}.tstconf =tstconf;
                    input{t}.func = func;
                    input{t}.rnd = rnd;
                    input{t}.range = range;
                    input{t}.tstcount = tstcount;
                    input{t}.tst_numb = tst;
                    input{t}.methods = config.methods;
                    
                    t = t+1;
                end
            end
        end
    end
end
%input generation now is not parallel because of use external inputgen func
%TODO - in next releases make it paralell (sending input data to cluster is
%expensive
parfor i=1:1
    if localmode==false
        mkdir('tmp')
        delete('tmp/*');
    end;
end
for i=1:t-1
    [X,Y,Yr] = inputgen(input{i}.func.expression,str2double(input{i}.range.start),str2double(input{i}.range.end),input{i}.rnd.type,str2double(input{i}.rnd.dispersion),input{i}.tstcount);
    g{i,1} = X;
    g{i,2} = Y;
    g{i,3} = input{i};
    for q=1:length(input{i}.methods)
        m{q} = input{i}.methods(q).method;
    end
    g{i,4} = m;
    g{i,5} = Yr;
end

%Computing data on cluster

parfor i=1:t-1
    clf;
    hold on;
    plot(g{i,1},g{i,2}, 'o');
    X = g{i,1};
    Y = g{i,2};
    func = g{i,3}.func.expression;
    Yreal = g{i,5};
    plot(X,Yreal,'black');
    
    inputdata = g{i,3};
    methods = '';
    methods = g{i,4};
    for m=1:length(methods)
        %disp([methods{m}.name ' ' methods{m}.active]);
        if strcmp(methods{m}.active,'true')
            switch methods{m}.name
                case 'polynomial'        
                   regress(g{i,1},g{i,2},methods{m}.params);
                case 'spline'
                    spline(g{i,1},g{i,2},methods{m}.params);
                case 'forth'
                    forth(g{i,1},g{i,2},methods{m}.params);
                case 'simplex'
                    simplex(g{i,1},g{i,2},methods{m}.params);
                otherwise
                    disp('unkonwn');
            end
            %disp(methods{m}.active);
        end;
    end
    
    
    hold off;   
    filename = sprintf(['fig' int2str(i) '.jpg']);
    print('-djpeg80',filename);
end
%One-iter loop just for copying report plots to localhost
parfor i=1:1
    %You need to map /home/melhior/shares/ on VM to TV\rc\img\
    if localmode==false
        system('scp -r * melhior@77.47.207.84:/home/melhior/shares/tmp/');
        delete('tmp/*');
    end;
end
%Creating array of filenames for building report
currtime = strrep(datestr(now), ':', '-');
currtime = strrep(currtime, ' ', '_');
mkdir(['reports/' currtime '_html_files']);
for i=1:t-1    
    copyfile('tmp/*',['reports/' currtime '_html_files']);
    filename = [pathstr '\reports\' currtime '_html_files\fig' int2str(i) '.jpg'];
    filenames{i} = filename;
end
report('reports/report.rpt'); 
disp(toc);
delete('tmp/*');
rmpath(genpath('./'));